<style type="text/css">
	@import "<?php echo Yii::app()->params['DOMAIN'] ?>styles/editor.css";
</style>
	<h1><?php echo $this->title ?></h1>
	<?php echo !empty($errors) ? '<p>' . implode('<br />', $errors) . '</p>' : NULL ?>
	<form method="post" action="">
		<table class="t-data t-data-edit">
			<thead>
				<tr class="tr-titles">
					<td class="td-row-title">Параметры</td>
					<td>Значения</td>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>Название</td>
					<td>
						<?php echo !empty($errors['page_title']) ? $errors['page_title'] : NULL ?>
						<input type="text" class="i-text" name="page_title" value="<?php echo !empty($data->page_title) ? $data->page_title : NULL ?>"/>
					</td>
				</tr>
				<tr>
					<td>ID</td>
					<td>
						<?php echo !empty($errors['page_name']) ? $errors['page_name'] : NULL ?>
						<input type="text" class="i-text i-text-url" name="page_name" value="<?php echo !empty($data->page_name) ? $data->page_name : NULL ?>"/>
					</td>
				</tr>
				<tr>
					<td>Скрывать?</td>
					<td>
						<?php echo !empty($errors['page_hidden']) ? $errors['page_hidden'] : NULL ?>
						<label><input type="radio" name="page_hidden" value="1" <?php echo !empty($data->page_hidden) ? ' checked="checked"' : NULL ?>/> Да</label>
						<label><input type="radio" name="page_hidden" value="0" <?php echo empty($data->page_hidden) ? ' checked="checked"' : NULL ?>/> Нет</label>
					</td>
				</tr>
				<tr>
					<td>Приоритет</td>
					<td>
						<?php echo !empty($errors['page_priority']) ? $errors['page_priority'] : NULL ?>
						<input type="text" class="i-text i-text-date" name="page_priority" value="<?php echo !empty($data->page_priority) ? $data->page_priority : NULL ?>"/>
					</td>
				</tr>
				<tr>
					<td>Положение</td>
					<td>
						Сверху: <input type="number" class="i-text i-text-date" name="page_style_top" value="<?php echo !empty($data->page_style_top) ? $data->page_style_top : NULL ?>"/>
						&nbsp;&nbsp;&nbsp;
						Слева: <input type="number" class="i-text i-text-date" name="page_style_left" value="<?php echo !empty($data->page_style_left) ? $data->page_style_left : NULL ?>"/>
					</td>
				</tr>
				<tr>
					<td>Ширина</td>
					<td>
						<input id="pageWidth" type="range" min="300" max="800" class="i-text i-text-date" name="page_style_width" value="<?php echo !empty($data->page_style_width) ? $data->page_style_width : NULL ?>"/>
					</td>
				</tr>
				<tr>
					<td>Стиль</td>
					<td>
<?php
$styles = array(
	'b-text' => 'Текст',
	'b-brood' => 'Бумага',
	'b-brood b-brood-light' => 'Бумага светлая'
);
?>
						<select id="page_style_class" name="page_style_class">
<?php foreach ($styles as $key => $value): ?>
							<option value="<?php echo $key ?>"<?php if($key == $data->page_style_class): ?> selected="selected"<?php endif ?>><?php echo $value ?></option>
<?php endforeach ?>
						</select>
					</td>
				</tr>
				<tr>
					<td>Текст</td>
					<td>
						<?php echo !empty($errors['page_content']) ? $errors['page_content'] : NULL ?>
						<textarea id="pageContent" name="page_content_src" class="t-wysiwyg" cols="77" rows="20"><?php echo !empty($data->page_content_src) ? htmlspecialchars($data->page_content_src, ENT_QUOTES) : NULL ?></textarea>
						<br />
						<div class="b-preview">
							<div id="pageContentPreview"></div>
						</div>
						<div class="l-right"><span class="g-clickable" id="togglePageContentPreview">[Переключить редактор]</span></div>
					</td>
				</tr>
			</tbody>
			<tfoot>
				<tr>
					<td>
						<a href="<?php echo $this->name ?>">&lt;&lt;&lt; Назад</a>
					</td>
					<td>
						<input type="submit" class="i-submit" value="Сохранить"/>
						<input type="submit" class="i-submit" name="goto_files" value="Сохранить и перейти к файлам"/>
					</td>
				</tr>
			</tfoot>
		</table>
	</form>
<?php
/*echo AdminHelper::wysiwyg(array(
	'content_css' => '"' . Yii::app()->params['DOMAIN']  . 'styles/editor.css"',
	'setup' => 'function(ed){ed.onPreInit.add(function(ed){ed.dom.doc.body.className = "' . $data->page_style_class . '";})}',
));
*/
?>
<script type="text/javascript">
$(function()
{
	var pageContent = $('#pageContent');
	var pageContentPreview = $('#pageContentPreview');
	var pageWidth = $('#pageWidth');

	pageContentPreview.removeClass().addClass("<?php echo $data->page_style_class ?>");
<?php if(!empty($data->page_style_width)): ?>
	pageContentPreview.width(<?php echo $data->page_style_width ?>);
<?php endif ?>

	$('#togglePageContentPreview').toggle(function()
	{
		var content = pageContent.val();

		function rep(re, str)
		{
			content = content.replace(re, str);
		};

		rep(/[\n\r\t]/g, '');
		rep('<', '&lt;');
		rep('>', '&gt;');

		rep(/\[h1\](.*?)\[\/h1\]/gi, '<h1>$1</h1>');
		rep(/\[h2\](.*?)\[\/h2\]/gi, '<h2>$1</h2>');
		rep(/\[h3\](.*?)\[\/h3\]/gi, '<h3>$1</h3>');
		rep(/\[p\](.*?)\[\/p\]/gi,    '<p>$1</p>');

		rep(/\[b\](.*?)\[\/b\]/gi,    '<b>$1</b>');
		rep(/\[i\](.*?)\[\/i\]/gi,    '<i>$1</i>');

		rep(/\[ol\](.*?)\[\/ol\]/gi, '<ol>$1</ol>');
		rep(/\[ul\](.*?)\[\/ul\]/gi, '<ul>$1</ul>');
		rep(/\[ul=images\](.*?)\[\/ul\]/gi, '<ul class="ul-images">$1</ul>');
		rep(/\[li\](.*?)\[\/li\]/gi, '<li>$1</li>');

		rep(/\[table=([^\]]+)\](.*?)\[\/table\]/gi, '<table class="t-$1">$2</table>');

		rep(/\[row\](.*?)\[\/row\]/gi, function(str, row)
		{
			return '<tr><td>' + row.split('[|]').join('</td><td>') + '</td></tr>';
		});

		rep(/\[row=(:?(\d+)x)\](.*?)\[\/row\]/gi, function(str, Xx, X, row)
		{
			return '<tr><td colspan="' + X + '">' + row.split('[|]').join('</td><td colspan="' + X + '">') + '</td></tr>';
		});

		rep(/\[hrow\](.*?)\[\/hrow\]/gi, function(str, row)
		{
			return '<tr><th>' + row.split('[|]').join('</th><th>') + '</th></tr>';
		});

		rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi, '<a href="$1">$2</a>');
		rep(/\[url\](.*?)\[\/url\]/gi, '<a href="$1">$1</a>');

		rep(/\[br\s?\/?\]/gi, '<br />');
		rep(/\[hr\s?\/?\]/gi, '<hr />');

		rep(/\[img\](.*?)\[\/img\]/gi, '<img src="<?php echo Yii::app()->params['DOMAIN'] ?>$1" alt="" />');
		rep(/\[photo\](.*?)\[\/photo\]/gi, '<img class="i-photo" src="<?php echo Yii::app()->params['DOMAIN'] ?>photos/$1.jpg" alt="" />');
		rep(/\[photo=(left|center|right)\](.*?)\[\/photo\]/gi, '<img class="i-photo i-photo-$1" src="<?php echo Yii::app()->params['DOMAIN'] ?>photos/$2.jpg" alt="" />');

		rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi, '<a href="$1">$2</a>');
		rep(/\[url\](.*?)\[\/url\]/gi, '<a href="$1">$1</a>');

		rep(/\[spoiler=([^\]]+)\]/gi, '<div class="b-spoiler"><div class="b-spoiler-header">$1</div><div class="b-spoiler-content">');
		rep(/\[\/spoiler\]/gi, '</div></div>');

		rep(/\[xpage=([^\]]+)\](.*?)\[\/xpage\]/gi, '<a class="x-hash" xhash="w:$1" href="/$1/">$2</a>');
		rep(/\[xphoto\]([^-]*)-?(.*?)\[\/xphoto\]/gi, '<a href="<?php echo Yii::app()->params['DOMAIN'] ?>photos/$1/$2.jpg"><img class="i-photo" src="<?php echo Yii::app()->params['DOMAIN'] ?>photos/$1/$2-icon.jpg" width="150" height="113" alt="" /></a>');
		rep(/\[xphoto=(left|center|right)\]([^-]*)-?(.*?)\[\/xphoto\]/gi, '<a href="<?php echo Yii::app()->params['DOMAIN'] ?>photos/$2/$3.jpg"><img class="i-photo i-photo-$1" src="<?php echo Yii::app()->params['DOMAIN'] ?>photos/$2/$3-icon.jpg" width="150" height="113" alt="" /></a>');

		rep(/\[contacts=form\]/gi,
'<form id="feed_form" method="post" action="">' +
'	<div id="feed_message" class="b-message"></div>' +
'	<dl>' +
'		<dt>Имя:</dt>' +
'		<dd><input type="text" class="i-text" name="name" value="" /></dd>' +
'		<dt>Почта:</dt>' +
'		<dd><input type="text" class="i-text" name="mail" value="" /></dd>' +
'		<dt>Текст сообщения:</dt>' +
'		<dd><textarea name="message"></textarea></dd>' +
'		<dd><input type="submit" class="i-submit" value="Отправить сообщение" /></dd>' +
'	</dl>' +
'</form>');

		rep(/\[contacts=phone\]/gi, '<b id="phone">+38 (***) ***-**-**</b>');
		rep(/\[contacts=mail\]/gi, '<b><a id="mail" href="mailto:***@***.***">***@***.***</a></b>');

		pageContentPreview.html(content).show();
		pageContent.hide();
	}, function()
	{
		pageContentPreview.hide();
		pageContent.show();
	});

	pageContent.keyup(function()
	{
		var me = $(this);
		if(this.scrollHeight && (me.height() != this.scrollHeight))
		{
			var height = Math.min(200, this.scrollHeight);
			me.height(this.scrollHeight + 25 + 'px');
		}
	});

	pageWidth.keyup(function()
	{
		this.value = parseInt(this.value) || 0;
		if(this.value > 0)
		{
			pageContentPreview.width(this.value + 'px')
		}
	});

	$('#page_style_class').change(function()
	{
		pageContentPreview.removeClass().addClass(this.value);
	});
	
	$('div.b-spoiler > div.b-spoiler-header').live('click', function()
	{
		$(this).parent().children('div.b-spoiler-content').slideToggle();
	});
});
</script>
